home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 5
/
Skunkware 5.iso
/
src
/
X11
/
wais
/
waisgate
/
docid.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-09
|
11KB
|
375 lines
/* WIDE AREA INFORMATION SERVER SOFTWARE:
No guarantees or restrictions. See the readme file for the full standard
disclaimer.
*
* these are the methods for the DocID struct
* -brewster
*
* $Log: docid.c,v $
* Revision 1.6 92/05/10 14:42:57 jonathan
* Made a little safer on NULL docid's and such.
*
* Revision 1.5 92/04/01 17:07:25 jonathan
* Added copyDocID.
*
* Revision 1.4 92/02/21 11:05:03 jonathan
* added RCSIdent
*
* Revision 1.3 92/02/21 11:04:30 jonathan
* Added header, fixed $Log.
*
* Revision 1.2 92/02/12 13:17:44 jonathan
* Added $Log so RCS will put the log message in the header
*
*/
#ifndef lint
static char *RCSid = "$Header: /tmp_mnt/net/quake/proj/wais/wais-8-b5/ir/RCS/docid.c,v 1.6 92/05/10 14:42:57 jonathan Exp $";
#endif
#include <ctype.h>
#include "docid.h"
#include "irfileio.h"
#include "cutil.h"
#include "cdialect.h"
#define TESTDOC(doc) if((doc) == NULL) return NULL
/*---------------------------------------------------------------------------*/
/* get the server slot */
any* GetServer(doc)
DocID* doc;
{
TESTDOC(doc);
if(NULL != doc->distributorServer)
return(doc->distributorServer);
else return(doc->originalServer);
}
/*---------------------------------------------------------------------------*/
any* GetDatabase(doc)
DocID* doc;
{
TESTDOC(doc);
if(NULL != doc->distributorDatabase)
return(doc->distributorDatabase);
else return(doc->originalDatabase);
}
/*---------------------------------------------------------------------------*/
any* GetLocalID(doc)
DocID* doc;
{
TESTDOC(doc);
if(NULL != doc->distributorLocalID)
return(doc->distributorLocalID);
else return(doc->originalLocalID);
}
/*---------------------------------------------------------------------------*/
long GetCopyrightDisposition(doc)
DocID* doc;
{
if(doc)
return(doc->copyrightDisposition);
else return 0;
}
/*---------------------------------------------------------------------------*/
/* returns FALSE if it loses, TRUE if it wins */
long ReadDocID(doc,file)
DocID* doc;
FILE* file;
{
char temp_string[MAX_SYMBOL_SIZE];
if(FALSE == CheckStartOfStruct("doc-id", file)){
return(FALSE);
}
doc->originalServer = NULL;
doc->originalDatabase = NULL;
doc->originalLocalID = NULL;
doc->distributorServer = NULL;
doc->distributorDatabase = NULL;
doc->distributorLocalID = NULL;
doc->copyrightDisposition = COPY_WITHOUT_RESTRICTION;
while(TRUE){
long result = ReadSymbol(temp_string, file,
(long)MAX_SYMBOL_SIZE);
if(FALSE == result){
return(FALSE);
}
if(END_OF_STRUCT_OR_LIST == result){
break;
}
if(0 == strcmp(temp_string, ":original-server")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->originalServer = an_any;
}
else if(0 == strcmp(temp_string, ":original-database")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->originalDatabase = an_any;
}
else if(0 == strcmp(temp_string, ":original-local-id")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->originalLocalID = an_any;
}
else if(0 == strcmp(temp_string, ":distributor-server")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->distributorServer = an_any;
}
else if(0 == strcmp(temp_string, ":distributor-database")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->distributorDatabase = an_any;
}
else if(0 == strcmp(temp_string, ":distributor-local-id")){
any* an_any = (any*)s_malloc(sizeof(any));
ReadAny(an_any, file);
doc->distributorLocalID = an_any;
}
else if(0 == strcmp(temp_string, ":copyright-disposition"))
ReadLong(file,&doc->copyrightDisposition);
else{
SkipObject(file);
}
}
return(TRUE);
}
/*---------------------------------------------------------------------------*/
/* this writes a CDocID to a stream */
long WriteDocID(doc,file)
DocID* doc;
FILE* file;
{
WriteNewline(file);
WriteStartOfStruct("doc-id", file);
if (NULL != doc->originalServer){
WriteNewline(file);
WriteSymbol(":original-server", file); WriteAny(doc->originalServer, file);
}
if (NULL != doc->originalDatabase){
WriteNewline(file);
WriteSymbol(":original-database", file); WriteAny(doc->originalDatabase, file);
}
if (NULL != doc->originalLocalID){
WriteNewline(file);
WriteSymbol(":original-local-id", file); WriteAny(doc->originalLocalID, file);
}
if (NULL != doc->distributorServer){
WriteNewline(file);
WriteSymbol(":distributor-server", file); WriteAny(doc->distributorServer, file);
}
if (NULL != doc->distributorDatabase){
WriteNewline(file);
WriteSymbol(":distributor-database", file);
WriteAny(doc->distributorDatabase, file);
}
if (NULL != doc->distributorLocalID){
WriteNewline(file);
WriteSymbol(":distributor-local-id", file);
WriteAny(doc->distributorLocalID, file);
}
WriteNewline(file);
WriteSymbol(":copyright-disposition", file);
WriteLong(doc->copyrightDisposition, file);
return(WriteEndOfStruct(file));
}
/*---------------------------------------------------------------------------*/
static Boolean safeCmp _AP((any* a1,any* a2));
static Boolean safeCmp(a1,a2)
any* a1;
any* a2;
{
/* compare 2 any's, either of which may be NULL */
if (a1 == NULL && a2 == NULL)
return(true);
else if (a1 == NULL || a2 == NULL)
return(false);
else if (a1->size != a2->size)
return(false);
else if (memcmp(a1->bytes,a2->bytes,(size_t)a1->size) == 0)
return(true);
else
return(false);
}
/*---------------------------------------------------------------------------*/
Boolean cmpDocIDs(d1,d2)
DocID* d1;
DocID* d2;
{
if (safeCmp(d1->originalServer,d2->originalServer) &&
safeCmp(d1->originalDatabase,d2->originalDatabase) &&
safeCmp(d1->originalLocalID,d2->originalLocalID) &&
safeCmp(d1->distributorServer,d2->distributorServer) &&
safeCmp(d1->distributorDatabase,d2->distributorDatabase) &&
safeCmp(d1->distributorLocalID,d2->distributorLocalID) &&
d1->copyrightDisposition == d2->copyrightDisposition)
return(true);
else
return(false);
}
/*---------------------------------------------------------------------------*/
DocID*
makeDocID()
{
return((DocID*)s_malloc((size_t)sizeof(DocID)));
}
/*---------------------------------------------------------------------------*/
DocID*
copyDocID(doc)
DocID* doc;
{
DocID* result = NULL;
if(doc != NULL &&
(result = makeDocID()) != NULL) {
result->originalServer = duplicateAny(doc->originalServer);
result->originalDatabase = duplicateAny(doc->originalDatabase);
result->originalLocalID = duplicateAny(doc->originalLocalID);
result->distributorServer = duplicateAny(doc->distributorServer);
result->distributorDatabase = duplicateAny(doc->distributorDatabase);
result->distributorLocalID = duplicateAny(doc->distributorLocalID);
}
return result;
}
/*---------------------------------------------------------------------------*/
void freeDocID(doc)
DocID* doc;
{
if(doc) {
freeAny(doc->originalServer);
freeAny(doc->originalDatabase);
freeAny(doc->originalLocalID);
freeAny(doc->distributorServer);
freeAny(doc->distributorDatabase);
freeAny(doc->distributorLocalID);
s_free(doc);
}
}
/*---------------------------------------------------------------------------*/
#define DT_OriginalServer (data_tag)1
#define DT_OriginalDatabase (data_tag)2
#define DT_OriginalLocalID (data_tag)3
#define DT_DistributorServer (data_tag)4
#define DT_DistributorDatabase (data_tag)5
#define DT_DistributorLocalID (data_tag)6
#define DT_CopyrightDispostion (data_tag)7
DocID*
docIDFromAny(rawDocID)
any* rawDocID;
/* read from a z3950 docid to a docid structure */
{
DocID* docID;
char* buf;
TESTDOC(rawDocID);
buf = rawDocID->bytes;
docID = makeDocID();
while ((buf - rawDocID->bytes) < rawDocID->size)
{ data_tag tag = peekTag(buf);
switch (tag)
{ case DT_OriginalServer:
buf = readAny(&(docID->originalServer),buf);
break;
case DT_OriginalDatabase:
buf = readAny(&(docID->originalDatabase),buf);
break;
case DT_OriginalLocalID:
buf = readAny(&(docID->originalLocalID),buf);
break;
case DT_DistributorServer:
buf = readAny(&(docID->distributorServer),buf);
break;
case DT_DistributorDatabase:
buf = readAny(&(docID->distributorDatabase),buf);
break;
case DT_DistributorLocalID:
buf = readAny(&(docID->distributorLocalID),buf);
break;
case DT_CopyrightDispostion:
buf = readNum(&(docID->copyrightDisposition),buf);
break;
default:
freeDocID(docID);
return(NULL);
};
}
return(docID);
}
/*---------------------------------------------------------------------------*/
any*
anyFromDocID(docID)
DocID* docID;
/* write a docid structure to a buffer in z3950 format */
{
any* rawDocID = NULL;
char* buf = NULL;
char* data = NULL;
long size,bytesLeft;
if (docID == NULL)
return(NULL);
size = writtenAnySize(DT_OriginalServer,docID->originalServer);
size += writtenAnySize(DT_OriginalDatabase,docID->originalDatabase);
size += writtenAnySize(DT_OriginalLocalID,docID->originalLocalID);
size += writtenAnySize(DT_DistributorServer,docID->distributorServer);
size += writtenAnySize(DT_DistributorDatabase,docID->distributorDatabase);
size += writtenAnySize(DT_DistributorLocalID,docID->distributorLocalID);
size += writtenNumSize(DT_CopyrightDispostion,docID->copyrightDisposition);
data = s_malloc((size_t)(sizeof(char) * size));
buf = data;
bytesLeft = size;
buf = writeAny(docID->originalServer,DT_OriginalServer,buf,&bytesLeft);
buf = writeAny(docID->originalDatabase,DT_OriginalDatabase,buf,&bytesLeft);
buf = writeAny(docID->originalLocalID,DT_OriginalLocalID,buf,&bytesLeft);
buf = writeAny(docID->distributorServer,DT_DistributorServer,buf,
&bytesLeft);
buf = writeAny(docID->distributorDatabase,DT_DistributorDatabase,
buf,&bytesLeft);
buf = writeAny(docID->distributorLocalID,DT_DistributorLocalID,buf,
&bytesLeft);
buf = writeNum(docID->copyrightDisposition,DT_CopyrightDispostion,
buf,&bytesLeft);
rawDocID = makeAny(size,data);
return(rawDocID);
}
/*---------------------------------------------------------------------------*/